package com.ruoyi.framework.web.service;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.pay.domain.PayCoinUserForm;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.wq.domain.WqTuan;
import com.ruoyi.wq.domain.WqTuanHome;
import com.ruoyi.wq.domain.WqUserTuan;
import com.ruoyi.wq.service.IWqTuanHomeService;
import com.ruoyi.wq.service.IWqTuanService;
import com.ruoyi.wq.service.IWqUserTuanService;

import cn.hutool.core.date.DateUtil;

@Component
public class TuanServcie {
	
	@Autowired
	private IWqTuanService wqTuanService;
	
	@Autowired
	private IWqTuanHomeService wqTuanHomeService;
	
	@Autowired
	private IWqUserTuanService wqUserTuanService;
	
	@Autowired
	private ISysUserService sysUserService;
	
	@Autowired
    private CoinUserService coinUserService;
	
	public WqTuanHome saveTuanHome(WqTuanHome wqTuanHome) {
		Long userId = SecurityUtils.getUserId();
		// 创建团
		WqTuanHome tuanHome = wqTuanHomeService.getTuanHome(userId, wqTuanHome.getTuanId());
		if(tuanHome == null){
			WqTuan tuan = wqTuanService.getById(wqTuanHome.getTuanId());
			if(tuan.getTuanNum() >= tuan.getMaxNum()){
				throw new ServiceException("拼团已结束");
			}
			tuanHome = new WqTuanHome();
			tuanHome.setTuanId(wqTuanHome.getTuanId());
			tuanHome.setTitle(tuan.getTitle());
			tuanHome.setTuanTag(tuan.getTuanTag());
			tuanHome.setUserId(userId);
			tuanHome.setTuanCoin(tuan.getTuanCoin());
			tuanHome.setBuyCoin(tuan.getBuyCoin());
			tuanHome.setTuanType(tuan.getTuanType());
			tuanHome.setPayPrice(tuan.getPayPrice());
			tuanHome.setPrice(tuan.getPrice());
			tuanHome.setUserNum(tuan.getUserNum());
			tuanHome.setTuanHour(tuan.getTuanHour());
			Date tuanTime = DateUtil.offsetHour(new Date(), tuan.getTuanHour());
			tuanHome.setTuanTime(tuanTime);
			wqTuanHomeService.saveOrUpdate(tuanHome);
		}
		return tuanHome;
	}

	public void updateTuanUser(String tuanHomeId, Long userId) {
		QueryWrapper<WqUserTuan> qw = new QueryWrapper<>();
		WqUserTuan entity = new WqUserTuan();
		entity.setTuanHomeId(tuanHomeId);
		entity.setUserId(userId);
		qw.setEntity(entity);
		WqUserTuan userTuan = wqUserTuanService.getOne(qw);
		if(userTuan != null){
			if("0".equals(userTuan.getStatus())){
				// 充值趣豆
				WqTuanHome tuanHome = wqTuanHomeService.getById(userTuan.getTuanHomeId());
				if("0".equals(tuanHome.getTuanType())){
					PayCoinUserForm payCoinUserForm = new PayCoinUserForm();
			    	payCoinUserForm.setCoinNum(tuanHome.getBuyCoin());
			    	payCoinUserForm.setCoinType("0");
			    	payCoinUserForm.setRemark("拼团充值");
			    	payCoinUserForm.setUserId(userId);
			    	payCoinUserForm.setIsPay("1");
			    	coinUserService.savePayCoinUser(payCoinUserForm);
				}else{
					// 更新用户会员
					SysUser sysUser = sysUserService.getById(userId);
					Date vipStartTime = new Date();
					Date vipEndTime = DateUtil.offsetDay(vipStartTime, tuanHome.getBuyCoin());
					if(sysUser.getVipEndTime() != null){
						if(sysUser.getVipEndTime().compareTo(vipStartTime) > 0){
							vipEndTime = DateUtil.offsetDay(sysUser.getVipEndTime(), tuanHome.getBuyCoin());
						}
					}
					sysUser.setVipStartTime(vipStartTime);
					sysUser.setVipEndTime(vipEndTime);
					sysUserService.updateById(sysUser);
				}
			}
			userTuan.setStatus("1");
			wqUserTuanService.updateById(userTuan);
		}
	}
	
}
